//
//  AUEssenceTopicPictureView.swift
//  Alamofire
//
//  Created by Miss_Special on 2018/4/17.
//

import UIKit
import AwesomeUBaseModule
import Kingfisher

class AUEssenceTopicPictureView: UIView {
    
    // bundle
    fileprivate var bundle: Bundle {
        get {
            return Bundle.init(for: type(of: self))
        }
    }
    // 主图
    @IBOutlet weak var imageView: AnimatedImageView!
    // gif
    @IBOutlet weak var gif_imageView: UIImageView!
    // 大图按钮
    @IBOutlet weak var big_button: UIButton!
    // 播放按钮
    @IBOutlet weak var playButton: UIButton!
    
    var picture: AUEssenceTopicPictureModel? {
        didSet {
            guard let picture = picture, picture.height > 0 else {
                return
            }
            guard let imageUrl = URL(string: picture.big.first ?? "") else {
                return
            }
            playButton.isHidden = true
            gif_imageView.isHidden = true
            /// 1 下载图片
            imageView.kf.setImage(with: imageUrl, placeholder: AUHomeModule.imageFromBundle(bundle: bundle, imageName: "imageBackground"), options: nil, progressBlock: nil, completionHandler: nil)
            
            /// 2 截取图片
            let height = picture.height / picture.width * (au_kScreenWidth - 24 * au_kScreenScale)
            if height > 1000 {
                // 是大图
                big_button.isHidden = false
            } else {
                // 不是大图
                big_button.isHidden = true
            }
            
            /// 3 显示图片
        }
    }
    
    var picture_gif: AUEssenceTopicGifModel? {
        didSet {
            guard let gif = picture_gif, gif.height > 0 else {
                return
            }
            guard let imageUrl = URL(string: gif.images.first ?? "") else {
                return
            }
            print("---gif---\(Thread.current)")
            playButton.isHidden = true
            big_button.isHidden = true
            gif_imageView.isHidden = false
            imageView.kf.setImage(with: imageUrl, placeholder: AUHomeModule.imageFromBundle(bundle: bundle, imageName: "imageBackground"), options: [], progressBlock: nil, completionHandler: nil)
        }
    }
    
    var video: AUEssenceTopicVideoModel? {
        didSet {
            guard let video = video, video.height > 0 else {
                return
            }
            guard let imageUrl = URL(string: video.thumbnail.first ?? "") else {
                return
            }
            big_button.isHidden = true
            gif_imageView.isHidden = true
            playButton.isHidden = false
            /// 1 下载图片
            imageView.kf.setImage(with: imageUrl, placeholder: AUHomeModule.imageFromBundle(bundle: bundle, imageName: "imageBackground"), options: nil, progressBlock: nil, completionHandler: nil)
        }
    }
    
    class func pictureView() -> AUEssenceTopicPictureView {
        let bundle = Bundle(for: self)
        return bundle.loadNibNamed("AUEssenceTopicPictureView", owner: nil, options: nil)?.first as! AUEssenceTopicPictureView
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        frame.size.width = UIScreen.main.bounds.width
        gif_imageView.isHidden = true
        big_button.isHidden = true
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        initViewFromNib()
//        print("---frame---")
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
//        initViewFromNib()
//        print("---coder---")
    }
    
    // xib中加载xib, 使用该方法
    // file owner 的 custom class 标记为 AUEssenceTopicPictureView
    // 容器视图 不 标记
    private func initViewFromNib(){
        
//        let bundle = Bundle(for: type(of: self))
//        let nib = UINib(nibName: "AUEssenceTopicPictureView", bundle: bundle)
//        self.contentView = nib.instantiate(withOwner: self, options: nil).first as! UIView
//        self.contentView.frame = bounds
//        self.addSubview(contentView)
        
        // Do any additional setup after loading the view.
        
    }

}
